package com.ytzl.itrip.biz.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytzl.itrip.beans.model.ItripHotelOrder;
import com.ytzl.itrip.beans.model.ItripUser;
import com.ytzl.itrip.beans.model.ItripUserLinkUser;
import com.ytzl.itrip.beans.model.vo.*;
import com.ytzl.itrip.biz.service.ItripHotelOrderService;
import com.ytzl.itrip.biz.service.ItripHotelRoomService;
import com.ytzl.itrip.utils.common.*;
import org.codehaus.jackson.map.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by asus on 2018/7/19.
 */
@Controller
@RequestMapping("/api/hotelorder")
public class HotelOrderController {

    @Resource
    private ItripHotelRoomService itripHotelRoomService;
    @Resource
    private ItripHotelOrderService itripHotelOrderService;
    @Resource
    private ValidationUtil validationUtil;

    @RequestMapping(value = "/getpersonalorderlist",method = RequestMethod.POST)
    @ResponseBody
    public Dto getpersonalorderlist(@RequestBody JSONObject json,
                                    HttpServletRequest request) {
        Map<String , Object> map = new HashMap();
       if (json.getInteger("orderType") != -1) {
           map.put("orderType", json.getString("orderType"));
       }
       if (json.getInteger("orderStatus") != -1){
           map.put("orderStatus",json.getInteger("orderStatus"));
       }
       if (json.getString("orderNo") !=null){
           map.put("orderNo",json.getString("orderNo"));
       }
        if (json.getString("linkUserName") !=null){
            map.put("linkUserName",json.getString("linkUserName"));
        }
        if (json.getString("startDate") !=null){
            map.put("checkInDate",json.getString("startDate"));
        }
        if (json.getString("endDate") !=null){
            map.put("checkOutDate",json.getString("endDate"));
        }
       map.put("userId",request.getHeader("token").split("_")[3]);
        try {
            Page<List<ItripHotelOrder>> listPage = itripHotelOrderService.queryPageByMap(map,json.getInteger("pageNo"),json.getInteger("pageSize"));
            return DtoUtil.returnDataSuccess(listPage);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取个人列表错误 ","100501");
        }
    }

    @RequestMapping(value = "/getpersonalorderinfo/{orderId}",method = RequestMethod.GET)
    @ResponseBody
    public Dto getpersonalorderinfo(@PathVariable("orderId") String orderId){
        if (orderId!=null){
            try {
                PersonalOrderInfoVo personalOrderInfoVo =
                        itripHotelOrderService.getpersonalorderinfo(orderId);
                if (EmptyUtils.isNotEmpty(personalOrderInfoVo)){
                    return DtoUtil.returnDataSuccess(personalOrderInfoVo);
                }else {
                    return DtoUtil.returnFail("没有相关订单信息","100526");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("获取个人订单信息错误","100527");
            }
        }else {
            return DtoUtil.returnFail("请传递参数","100525");
        }
    }

    @RequestMapping(value = "/getpersonalorderroominfo/{orderId}",method = RequestMethod.GET)
    @ResponseBody
    public Dto getpersonalorderroominfo(@PathVariable("orderId") String orderId){
        orderId="144";
        if (orderId!=null){
            try {
                ItripHotelOrderRoomVo itripHotelOrderRoomVo =
                        itripHotelOrderService.getpersonalorderroominfo(orderId);
                if (EmptyUtils.isNotEmpty(itripHotelOrderRoomVo)){
                    return DtoUtil.returnSuccess("获取个人订单房型信息成功",itripHotelOrderRoomVo);
                }else {
                    return DtoUtil.returnFail("没有相关订单房型信息","100529");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("获取个人订单房型信息错误","100531");
            }
        }else {
            return DtoUtil.returnFail("请传递参数","1005230");
        }
    }

    @RequestMapping(value = "/getpreorderinfo",method = RequestMethod.POST)
    @ResponseBody
    public Dto getpreorderinfo(@RequestBody ValidateRoomStoreVo validateRoomStoreVo){
        if (EmptyUtils.isNotEmpty(validateRoomStoreVo)){
            if (EmptyUtils.isEmpty(validateRoomStoreVo.getHotelId())){
                return DtoUtil.returnFail("hotelId不能为空","100510");
            }
            if (EmptyUtils.isEmpty(validateRoomStoreVo.getRoomId())){
                return DtoUtil.returnFail("roomId不能为空","100511");
            }
            try {
                BookBeforeOrderInfoVo bookBeforeOrderInfoVo =
                        itripHotelOrderService.getpreorderinfo(validateRoomStoreVo);
                if (EmptyUtils.isNotEmpty(bookBeforeOrderInfoVo)){
                    if (bookBeforeOrderInfoVo.getStore()<1){
                        return DtoUtil.returnFail("暂时无房","100512");
                    }
                    return DtoUtil.returnSuccess("获取成功",bookBeforeOrderInfoVo);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("系统异常","100513");
            }
        }
        return DtoUtil.returnFail("抱歉，未查询到数据","100512");
    }


    @RequestMapping(value = "/addhotelorder",method = RequestMethod.POST)
    @ResponseBody
    public Dto addhotelorder(@RequestBody ItripAddHotelOrderVo itripAddHotelOrderVo,
                             HttpServletRequest request){
        ItripUser itripUser = validationUtil.getuser(request.getHeader("token"));
        if (EmptyUtils.isEmpty(itripUser)){
            return DtoUtil.returnFail("token失效","100000");
        }
        if (EmptyUtils.isNotEmpty(itripAddHotelOrderVo)){
            ItripHotelOrder itripHotelOrder = new ItripHotelOrder();
            BeanUtils.copyProperties(itripAddHotelOrderVo,itripHotelOrder);
            ValidateRoomStoreVo validateRoomStoreVo = new ValidateRoomStoreVo();
            BeanUtils.copyProperties(itripAddHotelOrderVo,validateRoomStoreVo);
            try {
                BookBeforeOrderInfoVo bookBeforeOrderInfoVo =
                        itripHotelOrderService.getpreorderinfo(validateRoomStoreVo);
               if (EmptyUtils.isNotEmpty(bookBeforeOrderInfoVo)){
                   if (bookBeforeOrderInfoVo.getStore()<1){
                       return DtoUtil.returnFail("库存不足","100507");
                   }
                   itripHotelOrder.setUserId(itripUser.getId());
                   StringBuffer sb = new StringBuffer();
                   for (ItripUserLinkUser user:itripAddHotelOrderVo.getLinkUser()){
                       if (sb.length()==0){
                           sb.append(user.getLinkUserName());
                       }else
                           sb.append(","+user.getLinkUserName());
                   }
                   itripHotelOrder.setLinkUserName(sb.toString());
                   itripHotelOrder.setPayAmount(
                           itripHotelRoomService.
                                   getById(itripAddHotelOrderVo.getRoomId()).
                                   getRoomPrice());
                   itripHotelOrderService.save(itripHotelOrder);
                   HotelOrderVo hotelOrderVo = new HotelOrderVo();
                   BeanUtils.copyProperties(itripHotelOrder,hotelOrderVo);
                   return DtoUtil.returnSuccess("生成订单成功",hotelOrderVo);
               }
               return DtoUtil.returnFail("库存不足","101507");
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("生成订单失败","100505");
            }
        }
        return DtoUtil.returnFail("不能提交空，请填写订单信息","100506");
    }

    @RequestMapping(value = "queryOrderById/{orderId}",method = RequestMethod.GET)
    @ResponseBody
    public Dto queryorderbyid(@PathVariable Long orderId){
        try {
            ItripHotelOrder byId = itripHotelOrderService.getById(orderId);
            if (null==byId){
                return DtoUtil.returnFail("没有查询到相应订单","100533");
            }
            QueryHotelVo queryHotelVo = new QueryHotelVo();
            BeanUtils.copyProperties(byId,queryHotelVo);
            String[] linkname = queryHotelVo.getLinkUserName().split(",");
            queryHotelVo.setItripOrderLinkUserList(itripHotelOrderService.
                    queryLinkUserList(byId.getUserId(),linkname));
            return DtoUtil.returnDataSuccess(queryHotelVo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常","100534");
        }
    }



}